from django.http import FileResponse
from django.shortcuts import render
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.authtoken.models import Token
from storage.permissions import IsOwner, IsAccess
# Create your views here.

from storage import serializers

class RegisterView(APIView):
    permission_classes = [AllowAny,]
    def post(self, request):
        serializer = serializers.RegisterSerializer(data=request.data)
        if serializer.is_valid():
            user = serializer.save()
            token, _ = Token.objects.get_or_create(user=user)
            return Response({
                'success': True,
                'message': "Success",
                'token': token.key
            }, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)



class LoginView(APIView):
    permission_classes = [AllowAny,]
    def post(self, request):
        serializer = serializers.LoginSerializer(data=request.data)
        if serializer.is_valid():
            response = serializer.save()
            if response['success']:
                token, _ = Token.objects.get_or_create(user=response['message'])
                return Response({
                    'success': True,
                    'message': "Success",
                    'token': token.key
                }, status=status.HTTP_200_OK)
            else:
                return Response(response, status=status.HTTP_401_UNAUTHORIZED)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)



class LogoutView(APIView):
    permission_classes = [IsAuthenticated,]
    def get(self, request):
        user = request.user
        Token.objects.get(user=user).delete()
        return Response({
            "success": True,
            "message": "Logout"
        })


class FileUploadView(APIView):
    permission_classes = [IsAuthenticated,]
    def post(self, request):
        serializer = serializers.FileUploadSerializer(data=request.data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)


class FileView(APIView):
    def get_permissions(self):
        if self.request.method == 'GET':
            return [IsAccess()]
        return [IsOwner()]

    def patch(self, request, file_id):
        data = request.data.copy()
        data.update({'file_id': file_id})
        serializer = serializers.FileRenameSerializer(data=data)
        if serializer.is_valid():
            response = serializer.save(user=request.user, file_id=file_id, name=data.get('name'))
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)

    def patch(self, request, file_id):
        data = request.data.copy()
        data.update({'file_id': file_id})
        serializer = serializers.FileRenameSerializer(data=data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)

    def delete(self, request, file_id):
        data = request.data.copy()
        data.update({'file_id': file_id})
        serializer = serializers.FileDeleteSerializer(data=data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)


    def get(self, request, file_id):
        data = request.data.copy()
        data.update({'file_id': file_id})
        serializer = serializers.FileDownloadSerializer(data=data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return FileResponse(response.file, filename=response.file_name)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)



class FileAccessView(APIView):
    permission_classes = [IsOwner]
    def post(self, request, file_id):
        data = request.data.copy()
        data.update({'file_id': file_id})
        serializer = serializers.AddAccessSerializer(data=data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)

    def delete(self, request, file_id):
        data = request.data.copy()
        data.update({'file_id': file_id})
        serializer = serializers.RemoveAccessSerializer(data=data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)


class AllUserFilesView(APIView):
    permission_classes = [IsAuthenticated]
    def get(self, request):
        serializer = serializers.AllUserFilesSerializer(data=request.data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)


class AllAccessFilesView(APIView):
    permission_classes = [IsAuthenticated,]

    def get(self, request):
        serializer = serializers.AllAccessFilesSerializer(data=request.data)
        if serializer.is_valid():
            response = serializer.save(user=request.user)
            return Response(response, status=status.HTTP_200_OK)
        else:
            return Response({
                'success': False,
                'message': serializer.errors
            }, status=status.HTTP_422_UNPROCESSABLE_ENTITY)